﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using CM.DL.DALC.NC03;
using DECOR.BL.BE;

namespace DECOR.DL.DALC.NC03
{
    public class StockDALC : IStockDALC
    {
        private string _connId = string.Empty;

        public static StockDALC Instance(string pConnId)
        {
            if (_instance == null)
            {
                lock (typeof(StockDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new StockDALC(pConnId);
                    }
                }
            }
            return _instance;
        }

        private StockDALC(string pConnId) { this._connId = pConnId; }
        private StockDALC() { }
        private static volatile StockDALC _instance = null;

        #region IStockDALC Members

        public List<DECOR.BL.BE.StockBE> buscarStock(DECOR.BL.BE.StockBE pObjStock)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            DestConfig dc = new DestConfig();
            StockBE obj = null;
            List<StockBE> listaResult = new List<StockBE>();
            string ex_result = "";

            try
            {
                //try
                //{
                //    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                //}
                //catch { }
                //RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_BUSCA_STOCK");
                function.SetValue("P_MATNR", pObjStock.CodMaterial);
                function.SetValue("P_WERKS", pObjStock.CodCentro);
                function.SetValue("P_LGORT", pObjStock.CodAlmacen);
                function.SetValue("P_CHARG", pObjStock.CodLote);
                function.SetValue("P_PRDHA", pObjStock.Jerarquia);
                function.SetValue("P_VKORG", pObjStock.OrgVentas);
                function.SetValue("P_VTWEG", pObjStock.CanalDist);

                function.Invoke(dest);

                IRfcTable STOCK = function.GetTable("T_STOCK");

                foreach (IRfcStructure item in STOCK)
                {
                    obj = new StockBE();

                    obj.CodMaterial = item["MATNR"].ToString();
                    obj.DescMaterial = item["MATNOM"].ToString();
                    obj.CodCentro = item["WERKS"].ToString();
                    obj.DescCentro = item["WERNOM"].ToString();
                    obj.CodAlmacen = item["LGORT"].ToString();
                    obj.DescAlmacen = item["LGONOM"].ToString();
                    obj.CodLote = item["CHARG"].ToString();
                    obj.StockDisponible = Convert.ToDecimal(item["STOCK_DI"].ToString());
                    obj.StockEnTransito = Convert.ToDecimal(item["STOCK_CU"].ToString());
                    obj.StockEntregaCliente = Convert.ToDecimal(item["STOCK_EC"].ToString());
                    obj.StockLibre = Convert.ToDecimal(item["STOCK_LU"].ToString());
                    obj.StockPedidoCliente = Convert.ToDecimal(item["STOCK_PC"].ToString());

                    listaResult.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return listaResult;
        }

        public List<DECOR.BL.BE.StockBE> buscarStockEC(DECOR.BL.BE.StockBE pObjStock)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            DestConfig dc = new DestConfig();
            StockBE obj = null;
            List<StockBE> listaResult = new List<StockBE>();

            try
            {
                //try
                //{
                //    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                //}
                //catch { }
                //RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_BUSCA_STOCK_EC");
                function.SetValue("P_MATNR", pObjStock.CodMaterial);
                function.SetValue("P_WERKS", pObjStock.CodCentro);
                function.SetValue("P_LGORT", pObjStock.CodAlmacen);
                function.SetValue("P_CHARG", pObjStock.CodLote);
                function.SetValue("P_PRDHA", pObjStock.Jerarquia);

                function.Invoke(dest);

                IRfcTable STOCK = function.GetTable("T_STOCK_EC");

                foreach (IRfcStructure item in STOCK)
                {
                    obj = new StockBE();

                    obj.CodMaterial = item["MATNR"].GetString();
                    obj.DescMaterial = item["MAKTX"].GetString();
                    obj.CodCentro = item["WERKS"].GetString();
                    obj.DescCentro = item["NAME1"].GetString();
                    obj.CodAlmacen = item["LGORT"].GetString();
                    obj.DescAlmacen = item["LGOBE"].GetString();
                    obj.CodLote = item["CHARG"].GetString();
                    obj.StockDisponible = Convert.ToDecimal(item["CDISP"].GetString());
                    obj.StockEnTransito = Convert.ToDecimal(item["MENGE"].GetString());
                    if (item["EINDT"].GetString() != "" && item["EINDT"].GetString() != "00000000")
                    {
                        obj.FechaStock = Convert.ToDateTime(item["EINDT"].GetString());
                    }
                    obj.UMB = item["MEINS"].GetString();

                    listaResult.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return listaResult;
        }

        #endregion

        public List<StockCursoBE> buscaStockECTRansito(string numMat, string matDesc, string numDoc, 
            DateTime fechaIni, DateTime fechaFin)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            StockCursoBE obj = null;
            List<StockCursoBE> listaResult = new List<StockCursoBE>();

            try
            {
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZCM_SD_011");
                function.SetValue("P_MATNR", numMat);
                function.SetValue("P_MAKTX", matDesc);
                function.SetValue("P_VBELN", numDoc);
                function.SetValue("P_LFDAT_INI", fechaIni);
                function.SetValue("P_LFDAT_FIN", fechaFin);

                function.Invoke(dest);

                IRfcTable STOCK = function.GetTable("T_STOCK_CURSO");

                foreach (IRfcStructure item in STOCK)
                {
                    obj = new StockCursoBE();

                    obj.EBELN = item["EBELN"].GetString();
                    obj.EBELP = item["EBELP"].GetString();
                    obj.EDATU = item["EDATU"].GetString() == "0000-00-00" ? DateTime.Now : Convert.ToDateTime(item["EDATU"].GetString());
                    obj.LFDAT = item["LFDAT"].GetString();
                    obj.MAKTX = item["MAKTX"].GetString();
                    obj.MATNR = item["MATNR"].GetString();
                    obj.MEINS = item["MEINS"].GetString();
                    obj.MENGE = item["MENGE"].GetString();
                    obj.POSNR_VA = item["POSNR_VA"].GetString();
                    obj.POSNR_VL = item["POSNR_VL"].GetString();
                    obj.VBELN_VA = item["VBELN_VA"].GetString();
                    obj.VBELN_VL = item["VBELN_VL"].GetString();
                    obj.WADAT_IST = item["WADAT_IST"].GetString();

                    listaResult.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return listaResult;
        }
    }
}
